package cn.fuxue.hrms.web.admin_controller;

import cn.fuxue.hrms.common.dto.DepartmentShow;
import cn.fuxue.hrms.common.entity.Department;
import cn.fuxue.hrms.service.DepartmentService;
import cn.fuxue.hrms.service.impl.DepartmentServiceImpl;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/admin/department",produces ="text/plain;charset=UTF-8")
public class DepartmentController {

    @Autowired
    private DepartmentServiceImpl departmentService;


    @GetMapping("/find")
    @ResponseBody
    public String find(Model model) {
        List<DepartmentShow> departmentList=departmentService.find();
//        model.addAttribute("departmentList",departmentList);
//        return "/admin/department";
        return JSON.toJSONString(departmentList);
    }


    @GetMapping("/find/id")
    @ResponseBody
    public String findId(Model model) {
        return JSON.toJSONString(departmentService.findId());
    }

    @PostMapping("/add")
    public String add(
            @RequestParam("departmentName") String departmentName,
            @RequestParam("pid") Integer pid,
            @RequestParam("chairmanId") Integer chairmanId
    ) {
        Department department=new Department();
        department.setDepartmentName(departmentName);
        department.setChairmanId(chairmanId);
        department.setPid(pid);
        departmentService.insert(department);
        return "/admin/department";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        departmentService.deleteByPrimaryKey(id);
        return "admin/department";
    }


}
